package com.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.ConcurrentHashMap;

import com.netty.DiscardServerHandler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

public class BeanUtil {

	public static String convertByteArrayToHexStr(byte bytes[]) {

		StringBuffer sb = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			sb.append(convertDigitToHexChar((int) (bytes[i] >> 4)));
			sb.append(convertDigitToHexChar((int) (bytes[i] & 0x0f)));
		}
		return (sb.toString());

	}

	public static String convertByteArrayToHexStr(byte bytes[], int startIndex, int endIndex) {

		StringBuffer sb = new StringBuffer();
		for (int i = startIndex; i < endIndex; i++) {
			sb.append(convertDigitToHexChar((int) (bytes[i] >> 4)));
			sb.append(convertDigitToHexChar((int) (bytes[i] & 0x0f)));
		}
		return (sb.toString());

	}

	private static char convertDigitToHexChar(int value) {
		value = value & 0x0f;
		if (value >= 10)
			return ((char) (value - 10 + 'a'));
		else
			return ((char) (value + '0'));

	}

	public static void sendMessage() {
		ConcurrentHashMap<String, ChannelHandlerContext> channelMap = DiscardServerHandler.channelMap;
		if (channelMap.size() > 0) {
			ChannelHandlerContext ctx = channelMap.get("0000073699551617");
			Channel channel = ctx.channel();
			int i = 2000;
			// 把int转换为字节数组
			byte[] result = new byte[4];
			result[0] = (byte) (i >>> 24);// 取最高8位放到0下标
			result[1] = (byte) (i >>> 16);// 取次高8为放到1下标
			result[2] = (byte) (i >>> 8); // 取次低8位放到2下标
			result[3] = (byte) (i); // 取最低8位放到3下标
			ByteBuf encoded = ctx.alloc().buffer();
			encoded.writeBytes(result);
			encoded.retain();
			channel.write(encoded);
			channel.flush();
		}
	}

	public static void downloadFile() {
		ConcurrentHashMap<String, ChannelHandlerContext> channelMap = DiscardServerHandler.channelMap;
		if (channelMap.size() > 0) {
			ChannelHandlerContext ctx = channelMap.get("0000073699551617");
			Channel channel = ctx.channel();
			File file = new File("D:\\resource\\upgrade\\ECM0A001_2016062616481071.bin");
			long position = 0;

			int length = 0;
			byte[] sendByte = null;
			DataOutputStream dout = null;

			int packetSize = 128;
			long fileLength = 0;// fileLength
			int sumFileLength = 0;
			double scbl = 0; // 文件上传比例
			int scdx = 0; // 文件上传大小
			try {
				fileLength = file.length();

				fileLength = fileLength - position;
				System.out.println("一共" + fileLength + "个字节：");
				double douFileLength = fileLength;
				sumFileLength = (int) Math.ceil(douFileLength / 128);
				System.out.println("一共" + sumFileLength + "个包：");
				System.out.println("断点续传位置" + position);

				sendByte = new byte[packetSize];
				RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd");
				fileOutStream.seek(position);

				int index_ = 20;
				int k = 1;
				int sum = 65535;
				int y = 128 + index_;// 总长度
				int mantissa = 3;// 校验和尾数据长度
				int deviceTypeIndex = 1;// 类型
				while ((length = fileOutStream.read(sendByte, 0, sendByte.length)) > 0) {
					System.out.println(length);
					int i = 0;
					y = length + index_;
					byte[] bytes_one = new byte[y];
					bytes_one[i++] = (byte) 0xaa;
					bytes_one[i++] = (byte) 0x6e;
					bytes_one[i++] = (byte) 0;
					bytes_one[i++] = (byte) 0;
					bytes_one[i++] = (byte) 0x03;

					bytes_one[i++] = (byte) (deviceTypeIndex >> 8);
					bytes_one[i++] = (byte) deviceTypeIndex;

					bytes_one[i++] = (byte) (sumFileLength >> 8);// (byte)
																	// Integer.parseInt(0+""+date[0]);
					bytes_one[i++] = (byte) sumFileLength;// (byte)
															// Integer.parseInt(date[1]+""+date[2]);

					// 当前包长度
					bytes_one[i++] = (byte) (k >> 8);
					bytes_one[i++] = (byte) k;

					bytes_one[i++] = (byte) 0;
					bytes_one[i++] = (byte) (length + 4);

					// address
					sum = sum + length * k;
					bytes_one[i++] = (byte) (sum >> 24);
					bytes_one[i++] = (byte) (sum >> 16);
					bytes_one[i++] = (byte) (sum >> 8);
					bytes_one[i++] = (byte) (sum);
					// 数据包长度
					bytes_one[2] = (byte) (((i - 2) + length) >> 8);
					bytes_one[3] = (byte) ((i - 2) + length);
					
					int o = 0;
					for(int j = i; j < (y-mantissa) ;j++){
						bytes_one[j] = sendByte[o];
						o++;
					}

					int count = 0;
					for (int b = 4; b < bytes_one.length - 1; b++)
						count += bytes_one[b];
					// 校验checksum
					bytes_one[(y - mantissa)] = (byte) (count >> 8);
					bytes_one[(y - (mantissa - 1))] = (byte) count;

					bytes_one[(y - (mantissa - 2))] = (byte) 0x55;

					System.out.println(k + "==" + length + "=" + convertByteArrayToHexStr(bytes_one));
					scdx += length;
					scbl = (double) scdx / fileLength;
					int bl = (int) (scbl * 100);
					// dout.write(sendByte,0,length);
					ByteBuf encoded = ctx.alloc().buffer();
					encoded.writeBytes(bytes_one);
					encoded.retain();
					channel.write(encoded);
					channel.flush();
					Thread.currentThread();
					Thread.sleep(100);
					System.out.println("已上传：" + scdx + "字节,上传比例：" + bl + "%");
				}
				System.out.println("删除操作");

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
